package com.lysj.website.website.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.lysj.website.common.spring.base.BaseContent;
import com.lysj.website.common.web.ParamUtil;
import com.lysj.website.website.RandomConstant;
import com.lysj.website.website.domain.TodayBrowseAmount;
import com.lysj.website.website.service.TodayBrowseAmountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 16:10 2019/10/29
 * @ Description:网站浏览定时器
 **/
@Component
@Slf4j
public class WebsiteBrowseTask extends BaseContent {


    @Resource
    private TodayBrowseAmountService todayBrowseAmountService;


    @Scheduled(cron = "0 0/20 06-20 * * ?")
    public void queryOrder() {
        Integer initRandom = RandomUtil.randomInt(RandomConstant.RANDOM_START,RandomConstant.RANDOM_END);
        Integer growthRandom = RandomUtil.randomInt(RandomConstant.GROWTH_START, RandomConstant.GROWTH_END);
        //查询今日总的浏览数量
        TodayBrowseAmount todayBrowseAmount = todayBrowseAmountService.getRepository().findByCreateTimeBetween(DateUtil.beginOfMonth(new Date()).toJdkDate(), DateUtil.endOfMonth(new Date()).toJdkDate());
        if (ParamUtil.isBlank(todayBrowseAmount)) {
            todayBrowseAmount = new TodayBrowseAmount();
            todayBrowseAmount.setAmount(growthRandom);
            todayBrowseAmount.setCreateTime(new Date());
        }
        todayBrowseAmount.setAmount(todayBrowseAmount.getAmount() + initRandom);
        todayBrowseAmountService.save(todayBrowseAmount);

    }
}
